package com.webtest.authoritydemo;

import com.webtest.mvctest2.dao.DaoBase;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author liuziyang
 * @create 2018-08-19 16:10
 */
public class AuthorityDao extends DaoBase<Authority> {
    public List<Authority> getAllAuthority() {
        String sql = "SELECT id, displayName, url FROM authority";
        return getForList(sql);
    }

    public Authority getAuthorityById(long id) {
        String sql = "SELECT id, displayName, url FROM authority WHERE id=?";
        return get(sql, id);
    }

    public List<Authority> getAuthoritiesByIds(List<String> ids) {
        List<Authority> authorities = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(ids)) {
            ids.forEach(id -> {
                Authority authority = getAuthorityById(Long.parseLong(id));
                if (null != authority) {
                    authorities.add(authority);
                }
            });
        }
        return authorities;
    }
}
